package com.sykj.bsc.zn.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sykj.bsc.rwl.commons.ServerResponse;
import com.sykj.bsc.rwl.po.SysMenu;
import com.sykj.bsc.zjj.controller.ExportUtils;
import com.sykj.bsc.zn.bean.Bis_business_order;
import com.sykj.bsc.zn.bean.Bis_shop_user;
import com.sykj.bsc.zn.bean.FinanceUser;
import com.sykj.bsc.zn.service.znServiceImpl;
import com.sykj.bsc.zn.util.ViewExcel;

@Controller
@RequestMapping("proltal/zn1")
public class QtController {
	ObjectMapper om =new ObjectMapper();
	@Autowired
	znServiceImpl sus;
	
	@RequestMapping("iframe")
	public String iframe(){
		return "foreground/zn/iframe";
	}

	
	public static Object Md5Encrypt(String account,String password) {
		String algorithmName="Md5";//散列名称
		ByteSource salt=ByteSource.Util.bytes(account);//通过用户名加盐
		int hashIterations=1024;//散列次数
		Object result=new SimpleHash(algorithmName, password, salt, hashIterations);
		return result;
	}
	public static void main(String[] args) {
		Object result=Md5Encrypt("dz","123");
		System.out.println(result.toString());
	}
	
	//忘记密码页面
	@RequestMapping("forget")
	public String forget() {
		return "foreground/forgetPassword";
	}
	
	//生成验证码
	@RequestMapping("getcode")
	public @ResponseBody String getCode(String mobile) {
		Random r=new Random();
		String code="0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
		StringBuffer str=new StringBuffer();
		for(int i=0;i<4;i++) {
			str.append(code.charAt(r.nextInt(code.length())));
		}
		String data=str.toString();
		//IndustrySMS.execute(mobile, data);
		return data;
	}
	
	//跳转到修改密码界面
	@RequestMapping("resetPassword")
	public String resetPassword(Model model,@RequestParam("mobile") String mobile) {
		String account=sus.findAccount(mobile);
		model.addAttribute("account", account);
		model.addAttribute("mobile", mobile);
		return "foreground/resetPassword";
	}
	
	@RequestMapping("updatePassword")
	public @ResponseBody ServerResponse<String> updatePassword(String mobile,String password,String account){
		return sus.updatePssword(mobile, password, account);
	}
	
	//跳转到修改密码成功页面
		@RequestMapping("resetpasswordsuccess")
		public String resetpasswordsuccess() {
			return "foreground/resetpasswordsuccess";
		}
		
		//店主登录进去显示的页面
		@RequestMapping("findQt")
		public String findQt(Model model,HttpSession session){
			List<Map> list=sus.findQt(((com.sykj.bsc.zjj.bean.Bis_shop_user) session.getAttribute("protalUser")).getShop_user_id());
			model.addAttribute("list", list);
			String post_name=sus.findPost_name(((com.sykj.bsc.zjj.bean.Bis_shop_user) session.getAttribute("protalUser")).getPost_code());
			model.addAttribute("post_name", post_name);
			return "foreground/zn/Home";
		}
		//店主登录进去显示的页面
				@RequestMapping("findQt2")
				public String findQt2(Model model,HttpSession session){
					List<Map> list=sus.findQt2(((com.sykj.bsc.zjj.bean.Bis_shop_user) session.getAttribute("protalUser")).getShop_id());
					model.addAttribute("list", list);
					String post_name=sus.findPost_name(((com.sykj.bsc.zjj.bean.Bis_shop_user) session.getAttribute("protalUser")).getPost_code());
					model.addAttribute("post_name", post_name);
					return "foreground/zn/Home";
				}
		
		//通过店铺id查询用户信息
		@RequestMapping("findDianPu")
		public String findDianPu(Model model,String shop_id,HttpSession session) {
			List<SysMenu> list=sus.findUserMenu();
			model.addAttribute("list", list);
			session.setAttribute("shop_id",shop_id);
			return "foreground/index";
		}
		
		
		@RequestMapping("dianmian")
		public String dianmian(String shop_id,Map map) {
			map.put("d", sus.finddianmian(shop_id));
			return "foreground/dianmian";
		}
		
		//张诺前台财务
		private Map initMap(HttpServletRequest request){
			HttpSession session=request.getSession();
			Map<String,Object> map=new HashMap<String,Object>();
			String username=request.getParameter("username");
			if(username!=null){
				map.put("username",username);
			}
			String shop_id=(String)session.getAttribute("shop_id");
			if(shop_id!=null){
				map.put("shop_id",shop_id);
			}
			return map;
		}
		

		@RequestMapping("findbisinessordel")
		public String findbisinessordel(Model model,Bis_business_order businessorder,HttpSession session,HttpServletRequest request, @RequestParam(value = "page", defaultValue = "1") int page) {
			PageHelper.startPage(page,2);
			String shop_id=(String)session.getAttribute("shop_id");
			businessorder.setShop_id(Integer.valueOf(shop_id));
			List<Map> list=sus.findbisinessordel(businessorder);
			PageInfo<Map>  info=new  PageInfo<>(list);
			Map<String,Object> map=initMap(request);
			businessorder=sus.findgetorder(map);
			model.addAttribute("info", info);
			model.addAttribute("list2", businessorder);
			return "foreground/zn/qtcontrol";
		}
		
		@RequestMapping("/export2")//excel导出
		@ResponseBody
		public void report2(HttpServletRequest request,HttpServletResponse response)throws Exception{
			String arr=new String(request.getParameter("arr").getBytes("utf-8"), "utf-8");  
			String []str=arr.split("&");
			String[]title=str[0].split(",");
			String fileName = "信息表"+System.currentTimeMillis()+".xls";
			String sheetName = "信息表";	
			String [][]content=new String[str.length][title.length];
			for(int i=0;i<str.length-1;i++){
				content[i]=str[i+1].split(",");
			}
			HSSFWorkbook wb = ExportUtils.getHSSFWorkbook(sheetName, title, content, null);
			try {
				this.setResponseHeader(response, fileName);
		       OutputStream os = response.getOutputStream();
		       wb.write(os);
		       os.flush();
		      os.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}


		
		
		private Map<String, Object>  remap(HttpServletRequest request,HttpSession sess){
			Map<String, Object> map=new HashMap<String,Object>();
			String shop_id=(String)sess.getAttribute("shop_id");
			map.put("shop_id", shop_id);
			map.put("ptime", request.getParameter("ptime"));
			map.put("username", request.getParameter("username"));
			return map;
		}
		
		@RequestMapping("listuser")
		public ModelAndView listuser(HttpServletRequest request,@RequestParam(value="page",defaultValue="1")int page,HttpSession sess){
			ModelAndView md=new ModelAndView();
			Map<String, Object> map=this.remap(request,sess);
			 PageHelper.startPage(page, 2);
			 List<FinanceUser> list=sus.listfinanceuser(map);
			 PageInfo<FinanceUser> info=new PageInfo<>(list);
			  FinanceUser bo=sus.getfinanceuser(map);
			 md.addObject("info", info);
			 md.addObject("bo", bo);
			 md.addObject("map", map);
			 md.setViewName("foreground/zn/financeuser");
			return  md; 	
		} 

		@RequestMapping("/export")//excel导出
		@ResponseBody
		public void report(HttpServletRequest request,HttpServletResponse response)throws Exception{
			String arr=new String(request.getParameter("arr").getBytes("utf-8"), "utf-8");  
			String []str=arr.split("&");
			String[]title=str[0].split(",");
			String fileName = "信息表"+System.currentTimeMillis()+".xls";
			String sheetName = "信息表";	
			String [][]content=new String[str.length][title.length];
			for(int i=0;i<str.length-1;i++){
				content[i]=str[i+1].split(",");
			}
			HSSFWorkbook wb = ExportUtils.getHSSFWorkbook(sheetName, title, content, null);
			try {
				this.setResponseHeader(response, fileName);
		       OutputStream os = response.getOutputStream();
		       wb.write(os);
		       os.flush();
		      os.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		public void setResponseHeader(HttpServletResponse response, String fileName) {
	        try {
	            try {
	                fileName = new String(fileName.getBytes(),"utf-8");
	            } catch (Exception e) {
	                e.printStackTrace();
	            }
	            response.setContentType("application/octet-stream;charset=utf-8");
	            response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
	            response.addHeader("Pargam", "no-cache");
	            response.addHeader("Cache-Control", "no-cache");
	        } catch (Exception ex) {
	            ex.printStackTrace();
	        }
		
}
		
	
}
